<?php


namespace app\api\controller;


use app\BaseController;
use app\common\encryption\AlphaID;
use app\common\facade\auth\Auth;
use app\common\traits\Order;
use app\model\ChannelServDiscount;
use app\model\MovieOrder as MovieOrderModel;
use think\Request;
use app\model\CouponOrder as CouponOrderModel;
use app\api\service\MovieOrder;

class CouponOrder extends BaseController
{
    use Order;

    protected $middleware = [
        \app\middleware\ApiChannel::class    => ['except' => ['decryptCode']],
        \app\middleware\ApiUserAuthV2::class => ['except' => ['decryptCode']],
    ];




    //头部筛选
    public function getTopTab(Request $request)
    {

        $channel_id = $this->request->agentChannel['id'];
        $agent_id = $request->agentChannel['agent_id'];

        $trueServers = (new ChannelServDiscount())->getRealServ($channel_id,$agent_id);

        $model = ChannelServDiscount::query()
            ->field("serv_type")
            ->whereIn('serv_type',$trueServers)
            ->orderRaw('FIELD(serv_type, 15) desc') //指定排序
            ->union(\app\model\CouponOrder::query()->where('cnid',$channel_id)->where('user_id',Auth::id())->field('serv_type')->buildSql())
            ->append(['name', 'sub_tab'])
            ->select()->toArray();



        $waimai = [
            'serv_type' => '13,14',
            'name' => '外卖',
            'sub_tab' =>[
                [
                    'name' => '全部',
                    'value' => ''
                ],
                [
                    'name' => '已支付',
                    'value' => 'pay'
                ],
                [
                    'name' => '已完成',
                    'value' => 'finish'
                ],
                [
                    'name' => '退款',
                    'value' => 'refund'
                ],
            ],
        ];

        $hasTakeout = false;
        foreach ($model as $key=>$item) {
            if ($item['serv_type']  == PRODUCT_MEAL_SERV){
                unset($model[$key]);
            }

            if (in_array($item['serv_type'],[PRODUCT_ELE_SERV,PRODUCT_MEITUAN_TAKEOUT_SERV])){
                unset($model[$key]);
                $hasTakeout = true;
            }
        }

        if ($hasTakeout){
            $model[] = $waimai;
        }

        $res = array_values($model);

        return success('', $res);

    }

    /**
     * 解密code，查询对应的电影订单
     * @author ymzhang
     * @access public
     * @date 2022-08-15 15:00
     */
    public function decryptCode(Request $request)
    {
        $params = $request->all();
        $msg = dmjValidate([
            'code' => 'require',
        ], [], $params);
        if ($msg) {
            return $msg;
        }

        //解密
        $id = alphaID($params['code']);
        /**
         * @var MovieOrderModel $movieOrder
         * @var CouponOrderModel $couponOrder
         */
        //todo 暂时写死，调试使用，调试完成一定要去掉
        //$id = 70748;
        return (new MovieOrder())->getOrderDetail($id);
    }

    /**
     * 根据订单号查询对应的电影订单
     * @author ymzhang
     * @access public
     * @date 2022-08-17 11:10
     */
    public function ticketDetails(Request $request)
    {
        $params = $request->all();
        $msg = dmjValidate([
            'order_id' => 'require',
        ], [], $params);
        if ($msg) {
            return $msg;
        }
        //todo 暂时写死，调试使用，调试完成一定要去掉
        //$params['order_id'] = '210423102947392386';
        return (new MovieOrder())->getOrderDetail($params['order_id']);
    }

    public function showEnCode(Request $request)
    {
        $params = $request->all();
        $msg = dmjValidate([
            'code' => 'require',
        ], [], $params);
        if ($msg) {
            return $msg;
        }
        //https://hfcs.dmjvip.com/topup/order/peripheryOrderDetail?code=phJecf&channel=dmj
        /**
         * @var \app\model\CouponOrder $couponOrder
         */
        $couponOrder = \app\model\CouponOrder::query()->where('id', alphaID($params['code']))->find();
        if (empty($couponOrder)) {
            return fail('订单不存在');
        }

        $baseUrl = env('app.api_url');
        $headerUrl = $baseUrl.'/topup/order/peripheryOrderDetail?code='.$params['code'] . '&channel=' . $couponOrder->agent_channel->name;
        return redirect($headerUrl);
    }


}